//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var receiver_name=thisform.find('input[name=receiver_name]').val();
		var receiver_phone=thisform.find('input[name=receiver_phone]').val();
		var sender_phone=thisform.find('input[name=sender_phone]').val();
		var sender_name=thisform.find('input[name=sender_name]').val();
		$('#bagInboundDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#bagInboundDatagrid').datagrid({ 
			queryParams:{
				order_no:order_no,
				receiver_name:receiver_name,
				receiver_phone:receiver_phone,
				sender_phone:sender_phone,
				sender_name:sender_name
			}
		});
	})
	
	//更新行数据
	function bagInboundUpdateActions(value){
		$.post(bagInboundEditUrl,{id:value.id,id:value.storage_code,id:value.id},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#bagInboundDatagrid').datagrid('validateRow', editIndex)){
			$('#bagInboundDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#bagInboundDatagrid').datagrid('selectRow', index);
				$('#bagInboundDatagrid').datagrid('endEdit', editIndex);
				$('#bagInboundDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#bagInboundDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#bagInboundDatagrid').datagrid({
		//url:getbagInboundUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getbagInboundUrl,
		title:'包裹列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'单号',sortable:true},
		{field:'storage_code',title:'库位编号',sortable:true,
				editor:{
					type:'combobox',
					options:{
						valueField:'id',
						textField:'name',
						method:'get',
						url:storageListUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'receiver_name',title:'收件人姓名',sortable:true},
		{field:'receiver_address',title:'收件人地址',sortable:true},
		{field:'receiver_phone',title:'收件人电话',sortable:true},
		{field:'receiver_code',title:'收件人邮编',sortable:true},
		{field:'sender_name',title:'寄件人姓名',sortable:true},
		{field:'sender_address',title:'寄件人地址',sortable:true},
		{field:'sender_phone',title:'寄件人电话',sortable:true},
		{field:'sender_code',title:'寄件人邮编',sortable:true},
		{field:'mail_no',title:'面单号',sortable:true},
		{field:'weight',title:'包裹重量',sortable:true},
		{field:'price',title:'包裹单价',sortable:true},
		{field:'total_price',title:'包裹总资费',sortable:true},
		{field:'remark',title:'包裹备注',sortable:true},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'update_time',title:'更新时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					bagInboundUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
bagInboundObj={
	add:function(url){	//新增数据
		$('#bagInboundAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#bagInboundAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#bagInboundAddDialog').dialog('close');       
						$('#bagInboundDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	save:function(url){//编辑后保存数据
		$('#bagInboundEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#bagInboundEditDialog').dialog('close');
                    });
                }else{
                    $('#bagInboundEditDialog').dialog('close');      
                    $('#bagInboundDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#bagInboundDatagrid').datagrid('validateRow', editIndex)){
			$('#bagInboundDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#bagInboundDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#bagInboundDatagrid').datagrid({ queryParams:''});
	},
	/* infoBar:function(){//显示详情
		var bagInboundRow=$('#bagInboundDatagrid').datagrid('getSelections');
        if (bagInboundRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(bagInboundRow.length==1){
			var id=bagInboundRow[0]['id'];
        	showTab(bagInboundInfoUrl,'调拨单详情',id);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}, */
	reloadBar:function(){//刷新数据
		$('#bagInboundDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#bagInboundDatagrid').datagrid({ queryParams:{status:-1}});
	},
	addBar:function(){//显示添加框
		$('#bagInboundAddDialog').dialog('open').dialog('setTitle','添加');
		$('#bagInboundAddForm').form('clear');
		$('#bagInboundstorage').combobox('select',0);
		$('#inbound_order_no').textbox('setValue',inbound_order_no);
		// $("#inbound_order_no").textbox('textbox').attr('style','background:#ccc;height:30px;');
	},
	editBar:function(){//显示编辑框
		var bagInboundRow=$('#bagInboundDatagrid').datagrid('getSelections');
        if (bagInboundRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(bagInboundRow.length==1){            	
        	$('#bagInboundEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#bagInboundEditForm').form('load',bagInboundRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var bagInboundRow=$('#bagInboundDatagrid').datagrid('getSelections');
        if (bagInboundRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+bagInboundRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<bagInboundRow.length;i++){
                		ids.push(bagInboundRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(bagInboundDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#bagInboundAddDialog').dialog('close');       
								$('#bagInboundDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var bagInboundRow=$('#bagInboundDatagrid').datagrid('getSelections');
        if (bagInboundRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+bagInboundRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<bagInboundRow.length;i++){
                		ids.push(bagInboundRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(bagInboundClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#bagInboundAddDialog').dialog('close');       
								$('#bagInboundDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var bagInboundRow=$('#bagInboundDatagrid').datagrid('getSelections');
        if (bagInboundRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+bagInboundRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<bagInboundRow.length;i++){
                		ids.push(bagInboundRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(bagInboundUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#bagInboundAddDialog').dialog('close');       
								$('#bagInboundDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	examineBar:function(value){//审核数据
		var bagInboundRow=$('#bagInboundDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (bagInboundRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<bagInboundRow.length;i++){
                		ids.push(bagInboundRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(bagInboundExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#bagInboundDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}